package cn.eleven.dao.impl;

import cn.eleven.dao.RoomDAO;
import cn.eleven.entity.Room;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class RoomDAOImpl implements RoomDAO {

    /**
     * 查询所有房间信息
     */
    @Override
    public List<Room> findAll(Connection conn) throws SQLException {
        String sql = "SELECT id, name, status, capacity, price FROM room";
        // 定义在括号内的资源会在 try 块执行完毕后自动关闭，而不需要显式地调用 close() 方法
        // PreparedStatement 会提前编译 SQL 语句，参数的绑定也是通过 setXXX() 方法进行的，从而提高了性能，避免了 SQL 注入的风险
        try (PreparedStatement pstmt = conn.prepareStatement(sql)) {
            ResultSet rs = pstmt.executeQuery();  // 执行查询
            List<Room> rooms = new ArrayList<>();  // 用于存储查询到的房间列表
            while (rs.next()) {  // 遍历查询结果集
                Room room = new Room();
                room.setId(rs.getInt("id"));
                room.setName(rs.getString("name"));
                room.setStatus(rs.getString("status"));
                room.setCapacity(rs.getInt("capacity"));
                room.setPrice(rs.getDouble("price"));
                rooms.add(room);
            }
            return rooms;
        }
    }

    /**
     * 根据房间ID查询房间
     */
    @Override
    public Room findById(Connection conn, int id) throws SQLException {
        String sql = "SELECT id, name, status, capacity, price FROM room WHERE id = ?"; // 防止了 SQL 注入攻击
        // 定义在括号内的资源会在 try 块执行完毕后自动关闭，而不需要显式地调用 close() 方法
        try (PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setInt(1, id);  // 设置查询条件，房间ID
            ResultSet rs = pstmt.executeQuery();  // 执行查询
            if (rs.next()) {
                Room room = new Room();
                room.setId(rs.getInt("id"));
                room.setName(rs.getString("name"));
                room.setStatus(rs.getString("status"));
                room.setCapacity(rs.getInt("capacity"));
                room.setPrice(rs.getDouble("price"));
                return room;
            }
        }
        return null;
    }

    /**
     * 更新房间信息（状态和容量）
     */
    @Override
    public boolean update(Connection conn, Room room) throws SQLException {
        String sql = "UPDATE room SET status = ?, capacity = ? WHERE id = ?";
        // 定义在括号内的资源会在 try 块执行完毕后自动关闭，而不需要显式地调用 close() 方法
        try (PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setString(1, room.getStatus());
            pstmt.setInt(2, room.getCapacity());
            pstmt.setInt(3, room.getId());
            return pstmt.executeUpdate() > 0;
        }
    }

    /**
     * 更新房间状态
     */
    @Override
    public boolean updateStatus(Connection conn, Room room) throws SQLException {
        String sql = "UPDATE room SET status = ? WHERE id = ?";
        // 定义在括号内的资源会在 try 块执行完毕后自动关闭，而不需要显式地调用 close() 方法
        try (PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setString(1, room.getStatus());
            pstmt.setInt(2, room.getId());
            return pstmt.executeUpdate() > 0;
        }
    }

    /**
     * 将结果集中的一行数据映射为Room对象
     */
    private Room mapRowToRoom(ResultSet rs) throws SQLException {
        Room room = new Room();
        room.setId(rs.getInt("id"));
        room.setName(rs.getString("name"));
        room.setStatus(rs.getString("status"));
        return room;
    }
}
